﻿using System.Linq;
using System.Web.Mvc;
using OnlinePharmacyV001.Models;
using OnlinePharmacyV001.ViewModel;
using System.Data;

namespace OnlinePharmacyV001.Controllers
{
    public class ProductsController : Controller
    {
        OnlinePharmacyEntities db = new OnlinePharmacyEntities();
        //
        // GET: /Products/

        public ActionResult Index(int? pagesize, int? pageno, FormCollection c)
        {
            if (c.Count <= 0)
            {
                var model = new ProductViewModel();
                model.Products = db.Products.ToList();
                model.Categories = db.Categories.ToList();
                ViewData["keyword"] = string.Empty;
                ViewData["CateId"] = 0;
                ViewData["Status"] = 0;
                model.count = model.Products.Count;
                return View(model);
            }
            else
            {
                var model = new ProductViewModel();
                var key = Request["keyword"];
                var cateId = int.Parse(Request["cbxCateId"]);
                var status = int.Parse(Request["cbxStatus"]);
                model.Products = db.Products
                    .Where(p => !string.IsNullOrEmpty(key) ? p.prodName.Contains(key) || p.prodShortDes.Contains(key) : true)
                    .Where(p => status > 0 ? (status == 1 ? p.prodStatus == true : p.prodStatus == false) : true)
                    .Where(p => cateId > 0 ? p.cateId == cateId : true)
                    .ToList();
                model.Categories = db.Categories.ToList();
                model.count = model.Products.Count;
                ViewData["keyword"] = key;
                ViewData["CateId"] = cateId;
                ViewData["Status"] = status;
                return View(model);
            }
        }

        //
        // GET: /Products/Details/5

        public ActionResult Details(int id)
        {
            Product pro = db.Products.Find(id);
            return View(pro);
        }

        //
        // GET: /Products/Create

        public ActionResult Create()
        {
            ProductCreateViewModel model = new ProductCreateViewModel();
            model.Categories = db.Categories.ToList();
            return View(model);
        }

        //
        // POST: /Products/Create

        [HttpPost]
        public ActionResult Create(ProductCreateViewModel m)
        {
            try
            {
                // TODO: Add insert logic here

                if (ModelState.IsValid)
                {
                    int catid = int.Parse(Request["cbxCateId"]);
                    m.Product.cateId = catid > 0 ? catid : 1;
                    db.Products.Add(m.Product);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                m.Categories = db.Categories.ToList();
                return View(m);
            }
            catch
            {
                return View(m);
            }
        }

        //
        // GET: /Products/Edit/5

        public ActionResult Edit(int id)
        {
            Product p = db.Products.Find(id);
            return View(p);
        }

        //
        // POST: /Products/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Product p)
        {
            try
            {
                // TODO: Add update logic here

                if (ModelState.IsValid)
                {
                    db.Entry(p).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(p);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Products/Delete/5

        public ActionResult Delete(int id)
        {
            Product p = db.Products.Find(id);
            return View(p);
        }

        //
        // POST: /Products/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
